package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.SysBanner;
import com.yunkeji.dao.mapper.AppBannerMapper;
import com.yunkeji.dao.vo.AppBannerVo;
import com.yunkeji.system.domain.bo.AppBannerBo;
import com.yunkeji.system.service.IAppBannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * banner 配置Service业务层处理
 *
 * @author 老张
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class AppBannerServiceImpl implements IAppBannerService {

    private final AppBannerMapper baseMapper;

    /** 查询banner 配置 */
    @Override
    public AppBannerVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询banner 配置列表 */
    @Override
    public TableDataInfo<AppBannerVo> queryPageList(AppBannerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysBanner> lqw = buildQueryWrapper(bo);
        Page<AppBannerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询banner 配置列表 */
    @Override
    public List<AppBannerVo> queryList(AppBannerBo bo) {
        LambdaQueryWrapper<SysBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysBanner> buildQueryWrapper(AppBannerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysBanner> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCreateTime() != null, SysBanner::getCreateTime, bo.getCreateTime());
        lqw.eq(bo.getUpdateTime() != null, SysBanner::getUpdateTime, bo.getUpdateTime());
        lqw.eq(bo.getType() != null, SysBanner::getType, bo.getType());
        lqw.eq(bo.getStartTime() != null, SysBanner::getStartTime, bo.getStartTime());
        lqw.eq(bo.getStopTime() != null, SysBanner::getStopTime, bo.getStopTime());
        lqw.eq(bo.getJumpType() != null, SysBanner::getJumpType, bo.getJumpType());
        lqw.eq(
                StringUtils.isNotBlank(bo.getPictureUrl()),
                SysBanner::getPictureUrl,
                bo.getPictureUrl());
        lqw.eq(
                StringUtils.isNotBlank(bo.getBannerUrl()),
                SysBanner::getBannerUrl,
                bo.getBannerUrl());
        lqw.eq(bo.getSexGroup() != null, SysBanner::getSexGroup, bo.getSexGroup());
        lqw.eq(bo.getPlat() != null, SysBanner::getPlat, bo.getPlat());
        lqw.eq(bo.getAppType() != null, SysBanner::getAppType, bo.getAppType());
        lqw.eq(bo.getStatus() != null, SysBanner::getStatus, bo.getStatus());
        lqw.eq(bo.getSortIdx() != null, SysBanner::getSortIdx, bo.getSortIdx());
        return lqw;
    }

    /** 新增banner 配置 */
    @Override
    public Boolean insertByBo(AppBannerBo bo) {
        SysBanner add = BeanUtil.toBean(bo, SysBanner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改banner 配置 */
    @Override
    public Boolean updateByBo(AppBannerBo bo) {
        SysBanner update = BeanUtil.toBean(bo, SysBanner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(SysBanner entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除banner 配置 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
